<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title>添加考试</title>
	<style type="text/css">
	body center{
		width:1366px;
		height:768px;
		background:url(201.jpg);
		background-size:100% 100%;
		margin:0 auto;
	}	
	center{
	   padding-top:60px;
	}	   
	input{
		width:200px;
		height:40px;
		font-size:20px;
		padding-left:10px;
		margin-top:20px;
	}
	</style>
</head>
<body>
<form method="post" align="center">
    <div><?php if(isset($_POST['submit'])&&$_POST['search']!=null){echo "搜索结果为";}else{echo "请输入关键词搜索";}?></div>
        <div align="center">
        	<input type="text" name="search" value="<?php if(isset($_POST['submit'])&&$_POST['search']!=null){echo $_POST['search'];}else{echo "";}?>">
        	<button name="submit" type="submit">search</button><br>
        </div>
    </div> 
    <br>
    	<a href="tianjia.php">添加考试<br></a>  
    <br>         
</form>

<?php
include_once("conn.php");//链接数据库文件引用
if(isset($_POST['submit'])&&$_POST['search']!=null){
$sql="select * from anpai where name like '%{$_POST['search']}%'";
$rs=$pdo->query($sql);
$list=$rs->fetchAll(PDO::FETCH_ASSOC);
//先模糊搜索课程名字
$sql="select * from anpai where teacher like '%{$_POST['search']}%'";
$rs=$pdo->query($sql);
$list1=$rs->fetchAll(PDO::FETCH_ASSOC);
//再模糊搜索老师
$sql="select * from anpai where class like '%{$_POST['search']}%'";
$rs=$pdo->query($sql);
$list2=$rs->fetchAll(PDO::FETCH_ASSOC);
//再模糊搜索班级
$sql="select * from anpai where kaochang like '%{$_POST['search']}%'";
$rs=$pdo->query($sql);
$list3=$rs->fetchAll(PDO::FETCH_ASSOC);
//再模糊搜索考场
$all=array_merge($list,$list1,$list2,$list3);
//把得到的集合数组进行拼接
}else{
	$sql="select * from anpai";
	$rs=$pdo->query($sql);
	$all=$rs->fetchAll(PDO::FETCH_ASSOC);
}
?>

		
		
<table border="1" align="center">
	<thead>
		<tr>
			<th>id</th>
			<th>考试班级</th>
			<th>考试课程名称</th>
			<th>考试考场</th>
			<th>考试时间</th>
			<th>监考老师</th>
			<th>操作</th>
		</tr>
	</thead>
	<tbody>
	<?php 
	    // 这个分页效果的原理就是,截取数组来达到分页效果
		// 定义每页显示多少条
		$page=10;
	    // 获取数据总条数
		$total=count($all);
		// 计算总页数香上取整
		$pageall=ceil($total/$page);
		// 当前页,后面解释如果当前页不存在就命令它为1
		$dpage = $_GET['page'] ?? 1;
		//上一页
		//若减一后小于等于一就强制让他等于一,否者才能进行减一操作
		$pageprev=$dpage-1 <= 1 ? : ($dpage-1);
		//下一页
		// 如果加一后大于当前页的总页数$pageall就让他强行等于$pageall 否者才能进行加一操作
		$pagenext = $dpage + 1 >= $pageall ? $pageall : ($dpage + 1);
		//通过array_slice截取数据
		// $num当前页面第一条数据的数值
		$num=($dpage-1)*$page;
		// 截取数组来达到分页效果
		$count=array_slice($all,$num,$page,true);
		// 分页效果结束
	?>
	<?php
		foreach($count as $key => $value){
	?>
		<tr>
			<td><?php echo $value['id']?></td>
			<td><?php echo $value['class']?></td>
			<td><?php echo $value['name']?></td>
			<td><?php echo $value['kaochang']?></td>
			<td><?php echo $value['time']?></td>
			<td><?php echo $value['teacher']?></td>
			<td><a href=admin.php?action=admin&id="<?php echo $value['id']?>">修改</a>/
			<a href=delete.php?action=del&id="<?php echo $value['id']?>" onclick='return del();'>删除</a></td>
		</tr>
	<?php
		}
	?>
	</tbody>
</table>

<div align="center">
<?php
		echo $total.'条/当前'.$dpage.'页/共'.$pageall.'页&nbsp;&nbsp;';
		echo '<a href="?page=1">首页</a>&nbsp;&nbsp;';
		echo '<a href="?page='.$pageprev.'">上一页</a>&nbsp;&nbsp;';
		echo '<a href="?page='.$pagenext.'">下一页</a>&nbsp;&nbsp;';
		echo '<a href="?page='.$pageall.'">尾页</a>&nbsp;&nbsp;';
?>	<div>
</body>
</html>